package edu.wayne.cs.csc4110.hw6.framework.gui.inbox;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import edu.wayne.cs.csc4110.hw6.framework.Util.Constants;
import edu.wayne.cs.csc4110.hw6.framework.gui.lobby.GameButton;
import edu.wayne.cs.csc4110.hw6.framework.message.MessageServer;
import edu.wayne.cs.csc4110.hw6.interfaces.message.PrivateMessage;
import edu.wayne.cs.csc4110.hw6.interfaces.user.User;

public class MessagesPanel extends JPanel {

	private JPanel messagesContainer;
	private List<PrivateMessage> messages;
	private JScrollPane messageScroll;


	public MessagesPanel(final User user) {
	
		
		messagesContainer = new JPanel();
		messagesContainer.setLayout(new BoxLayout(messagesContainer, BoxLayout.Y_AXIS));
		messages = MessageServer.getInstance().getMessages(user);
		this.setPreferredSize(Constants.INBOX_MESSAGES_PREFRERRED_SIZE);
		messageScroll = new JScrollPane(messagesContainer);
		messageScroll.setAutoscrolls(true);
		if (messages != null) {
			for (PrivateMessage msg : messages) {
				JTextArea msgContent = new MessageTextArea("Sender:"+msg.getSender().getUsername() + "\n" + "Subject"+msg.getSubject()+ "\n"+"Message:"+ msg.getFormatedMessage());
				msgContent.setPreferredSize(new Dimension(300,100));
				JScrollPane textScroll = new JScrollPane(msgContent);
				textScroll.setAutoscrolls(true);
				JButton replyBtn = new GameButton("Reply");
				JPanel msgpanel = new JPanel();
			

				msgpanel.add(textScroll);
				msgpanel.add(replyBtn);
				replyBtn.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								new PrivateChatFrame(user).setVisible(true);
							}
						});
					}
				});
				msgpanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
				messagesContainer.add(msgpanel);
			}
		}

		this.add(messageScroll);
	}
}
